package com.automannn.practice.mybatis.springMybatis;

import com.automannn.practice.mybatis.dao.PersonDao;
import com.automannn.practice.mybatis.entity.PersonEntity;
import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * @author chenkh
 * @time 2021/11/16 16:14
 */
@ComponentScan
public class SpringMybatisTest {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringMybatisTest.class);

        //第一种，直接通过sql查询
       SqlSession sqlSession= (SqlSession) applicationContext.getBean("sqlSessionTestBySql");
        List list =  sqlSession.selectList("com.automannn.practice.mybatis.dao.PersonDao.selectList");
        System.out.println(list);

        System.out.println("========通过接口查询============");
        //第二种，获取接口查询
        System.out.println(applicationContext.getBean(PersonDao.class));
        PersonDao personDao= sqlSession.getMapper(PersonDao.class);
        System.out.println(personDao.selectList());



    }

    @Component
    public static class MybatisConfig{

        @Bean
        public SqlSessionFactoryBean sqlSessionFactoryBean(){
            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
            Resource resource = new ClassPathResource("org/mybatis/example/PersonMapper.xml");
            sqlSessionFactoryBean.setMapperLocations(resource);
            PooledDataSource dataSource = new PooledDataSource("com.mysql.cj.jdbc.Driver","jdbc:mysql://localhost:3306/zhenhe?useUnicode=true;serverTimezone=CST;characterEncoding=utf-8","root","chenkaihai");

            Properties properties = new Properties();
            properties.put("useUnicode","true");
            properties.put("serverTimezone","CST");
            properties.put("characterEncoding","utf-8");
            properties.put("dbname","zhenhe");

            dataSource.setDriverProperties(properties);
            sqlSessionFactoryBean.setDataSource(dataSource);
            return sqlSessionFactoryBean;
        }

        @Bean
        public SqlSessionTemplate sqlSessionTestBySql(SqlSessionFactoryBean factoryBean){
            try {
                return new SqlSessionTemplate(factoryBean.getObject());
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Bean
        /*xxx: spring通过工厂类实现的自动注入: MapperFactoryBean,最终依然是通过 sqlSession获取的对象*/
        public MapperScannerConfigurer mybatisScanner(){
            MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
            mapperScannerConfigurer.setBasePackage("com.automannn.practice.mybatis.dao");
            return mapperScannerConfigurer;
        }



    }
}
